<?php
define('CORE_DIR',  		__Dir__ . '/../') ;
define('DATASTORAGE_DIR',  	CORE_DIR .'Datastorage/');
define('COMMONS_DIR',  		CORE_DIR .'CoreCommons/');
define('CONTRACTS_DIR',  	CORE_DIR .'CoreContracts/');


require_once(COMMONS_DIR . 'logger.class.php');
require_once(COMMONS_DIR . 'serviceresponse.class.php');
require_once(COMMONS_DIR . 'parameters.class.php');
require_once(DATASTORAGE_DIR . 'session.class.php');
require_once(CONTRACTS_DIR . 'tchatmessage.entity.php');
require_once(CONTRACTS_DIR . 'user.entity.php');

echo 'Process clean tchat messages table <br />';

/*
* Clean the tchat messages table
*/
function cleanTchatTable()
{
	echo "cleanTchatTable started <br />";
	
	$response = false;
	
	try
	{	
		$session = Core\Datastorage\Session::Instance();
		$session->Open();

		$session->CreateCriteria('TchatMessage');
		$messages = $session->ListResult();
		
		foreach($messages as $message)
		{
			echo 'Delete ' . $message->Message;
			$session->Delete($message);
		}
		
		echo count($messages) . " messages <br />";
		
		$session->Close();
		echo "cleanTchatTable finished <br />";
		
		$response = true;
	}
	catch (\Exception $ex) 
	{
		echo $ex->getMessage() . "<br />";
	}
	
	return $response;
}

$response = cleanTchatTable();

if($response == false)
{
	echo "cleanTchatTable is failed <br />";
}

?>